package com.hspedu.jichu.encapsulation;

public class Encap01 {
    public static void main(String[] args) {

        Person p = new Person();
        p.setAge(100);// ctrl + B 转到方法声明
        p.setJob("程序员");
        p.setName("jack");
        p.setSalary(100000);
        p.show();
    }
}


class Person{
//封装
//对属性进行私有化
//给两个公共方法进行属性的访问与修改
    private String name;
    private int age;
    private double salary;
    private String job;


    public Person() {
    }
    //构造器与getXxx方法setXxx方法结合
    //为防止通过有参构造器，绕过setXxx方法
    public Person(String name, int age, double salary, String job) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
//        this.job = job;
        setName(name);
        setAge(age);
        setSalary(salary);
        setJob(job);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length()>2 && name.length()<=6){
            this.name = name;
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>=1 && age<=120){
            this.age = age;
        }else {
            System.out.println("年龄不合法");
        }
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }


    public void show(){
        System.out.println("name=" + name + " age=" + age + " salary=" + salary + " job=" + job);
    }
}